#include "jogo.h"

int xspeed_tmp = 0, yspeed_tmp = 0;
int pontuacao = 0;
int pato = 0, patos_total = 0;
int x_tmp = 50, y_tmp = 10;

// definicao dos sons
Note kill[] = {{200.02, 2}, {235.02, 2}};
Note new[] = {{329.63, 3}, {392, 6}};
Note away[] = {{237.02, 1}, {329.63, 1}};
Note shoot[] = {{200.02, 2}};
Note ndog1[] = {{1318.51, 2}};
Note ndog2[] = {{1396.91, 2}};
Note ndog3[] = {{1318.51, 2}};
Note ndog4[] = {{1244.51, 2}};
Note dogwin[] = {{1318.51, 2}, {1500.51, 2}, {1700.51, 2}, {1318.51, 2}, {1500.51, 2}, {1700.51, 2}};
Song skill = {sizeof(kill)/sizeof(Note), 1, kill};
Song snew = {sizeof(new)/sizeof(Note), 1, new};
Song saway = {sizeof(away)/sizeof(Note), 1, away};
Song sshoot = {sizeof(shoot)/sizeof(Note), 1, shoot};
Song sdog1 = {sizeof(ndog1)/sizeof(Note), 1, ndog1};
Song sdog2 = {sizeof(ndog2)/sizeof(Note), 1, ndog2};
Song sdog3 = {sizeof(ndog3)/sizeof(Note), 1, ndog3};
Song sdog4 = {sizeof(ndog4)/sizeof(Note), 1, ndog4};
Song sdogwin = {sizeof(dogwin)/sizeof(Note), 1, dogwin};


void move_duck(Sprite *s, char* base){
	draw_sprite(s, base);
	
	if(s->x + s->width > 800) xspeed_tmp *= -1;
	else if(s->x < 0) xspeed_tmp *= -1;
	
	if(s->y + s->height > 400) yspeed_tmp *= -1;
	else if(s->y < 0) yspeed_tmp *= -1;
	
	animate_sprite(s, base);
}


void move_cross(Sprite *s, char* base){
	
	if(s->x >= 800) s->x = 800;
	if(s->x <= 0) s->x = 0;
	if(s->y >= 600) s->y = 600;
	if(s->y <= 0) s->y = 0;
	
	draw_sprite(s, base);
}


void kill_duck(Sprite *s, char* base){
	Sprite *pato = create_sprite(duck_shot);
	Sprite *win = create_sprite(dog_win);
	pato->x = s->x;
	pato->y = s->y;
	
	win->x = 350;
	win->y = 295;

	play_song(&skill);
	
	draw_sprite_scaled(pato, base, s->width, s->height);
	delay(300);
	delete_sprite(pato, base);
	
	while(s->y <= 400){
		drawStringAt("SHOT DOWN!", 320, 270, 1, 53, 2, base, table);
		delete_sprite(s, base);
		s->y += 10;
		draw_sprite(s, base);
		delay(20);
	}
	
	delete_sprite(s, base);
	
	draw_sprite(win, base);
	play_song(&sdogwin);
	delay(500);
	delete_sprite(win, base);
	
	pontuacao += 100;
	
}


void fly_away(Sprite *s, char* base){
	Sprite *riso = create_sprite(riso1);
	Sprite *riso3 = create_sprite(riso2);
	riso->x = 350;
	riso->y = 270;
	riso3->x = 350;
	riso3->y = 270;
	
	play_song(&saway);
	
	while(s->y >= 0 && s->x >= 0){
		drawStringAt("FLY AWAY!", 340, 240, 1, 53, 2, base, table);
		delete_sprite(s, base);
		s->y -= 10;
		s->x -= 4;
		draw_sprite(s, base);
		delay(20);
	}
	
	int i=0, j=1;
	
	while(i != 10){
		if(i == 0 || i == 4 || i == 8) play_song(&sdog1);
		if(i == 1 || i == 5 || i == 9) play_song(&sdog2);
		if(i == 2 || i == 6) play_song(&sdog3);
		if(i == 3 || i == 7) play_song(&sdog4);
		delay(30);
		
		if(j > 0)
			draw_sprite_scaled(riso, base, 100, 150);
		else
			draw_sprite_scaled(riso3, base, 100, 150);
		
		j *=-1;
		i++;
	}
	destroy_sprite(riso);
	destroy_sprite(riso3);
}

void draw_dog(char* base){
	Sprite *cao = create_sprite(dog1);	
	Sprite *cao2 = create_sprite(dog2);
	cao2->x = 200;
	cao2->y = 350;
	cao->x = 0;
	cao->y = 400;
	cao2->xspeed = 20;
	cao2->yspeed = -50;
	cao->xspeed = 20;
	cao->yspeed = 0;
	//cao a andar
	while(cao->x <= 200){
		draw_sprite(cao, base);
		play_song(&sshoot);
		animate_sprite(cao, base);
		delete_sprite(cao, base);
	}
	//cao a saltar
	int tam_x = cao2->width, tam_y = cao2->height;
	while(cao2->y >= 200){
		draw_sprite_scaled(cao2, base, tam_x, tam_y);
		delay(50);
		animate_sprite(cao2,base);
		delete_sprite(cao2, base);
		tam_x -= 5;
		tam_y -= 5;
	}
	cao2->yspeed = 20;
	while(cao2->y <= 300){	
		draw_sprite_scaled(cao2, base, tam_x, tam_y);
		delay(50);
		animate_sprite(cao2,base);
		delete_sprite(cao2, base);
		tam_x -= 5;
		tam_y -= 5;
	}
	destroy_sprite(cao);
	destroy_sprite(cao2);
}


RTC_TIME timer(){

	RTC_TIME rt;
	rtc_read_time(&rt);

	return rt;
}


void std_screen(Sprite *back, char* base, int nivel){
	x_tmp = 200;
	y_tmp = 200;
	
	draw_sprite(back, base);
	drawStringAt("Pontuacao: ", 500, 500, 1, 44, 1, base, table);
	drawIntAt(pontuacao, 585, 500, 1, 44, 1, base, table);
	drawStringAt("Patos: ", 500, 520, 1, 44, 1, base, table);
	drawIntAt(pato, 585, 520, 1, 44, 1, base, table);
	drawStringAt("/", 600, 520, 1, 44, 1, base, table);
	drawIntAt(patos_total, 612, 520, 1, 44, 1, base, table);
	drawStringAt("Nivel: ", 500, 540, 1, 44, 1, base, table);
	drawIntAt(nivel, 585, 540, 1, 44, 1, base, table);
	
	//delay(1500);
	play_song(&snew);
	rtc_ini = timer();
}



void animate_game(int xspeed, int yspeed, int nivel, char* base){

	load_char_table(table);

	// definicao das sprites
	Sprite *duck1_left = create_sprite(duck1_l);
	Sprite *duck1_right = create_sprite(duck1_r);
	Sprite *cross1 = create_sprite(cross);
	Sprite *back = create_sprite(background);
	Sprite *down = create_sprite(duck1_down);
	Sprite *fly = create_sprite(duck1_flyaway);
	
	// iniciacao
	xspeed_tmp = xspeed;
	yspeed_tmp = yspeed;
	pato = 0;
	patos_total = 0;
	
	draw_dog(base);
	draw_sprite(back, base);
	//delay(1000);
	
	drawStringAt("NIVEL ", 350, 240, 1, 53, 2, base, table);
	drawIntAt(nivel, 450, 240, 1, 53, 2, base, table);
	delay(1500);
	std_screen(back, base, nivel);
	
	int i = 0, dx, dy;
	
	while(i != 10 && gameover != 1){
		patos_total = i + 1;
		// iniciacao do timer
		rtc_tmp = timer();
		
		if(scancode == 1) gameover = 1;
		
		// atribuicoes
		duck1_left->x = x_tmp;
		duck1_left->y = y_tmp;
		duck1_left->xspeed = xspeed_tmp;
		duck1_left->yspeed = yspeed_tmp;
		duck1_right->x = x_tmp;
		duck1_right->y = y_tmp;
		duck1_right->xspeed = xspeed_tmp;
		duck1_right->yspeed = yspeed_tmp;
		
		// condicoes para fly away
		if(((rtc_tmp.hour == rtc_ini.hour) && (rtc_tmp.min == rtc_ini.min) && (rtc_tmp.sec - rtc_ini.sec >= 7)) || ((rtc_tmp.hour == rtc_ini.hour) && (rtc_tmp.min > rtc_ini.min) && ((rtc_tmp.sec + 60) - rtc_ini.sec >= 7))){
			fly->x = duck1_left->x;
			fly->y = duck1_left->y;
			fly->xspeed = duck1_left->xspeed;
			fly->yspeed = duck1_left->yspeed;
			fly_away(fly, base);
			std_screen(back, base, nivel);
			i++; }

		// verificacoes do timer
		if(rtc_tmp.sec > 59) { rtc_tmp.sec -= 60; rtc_tmp.min++; }
		if(rtc_tmp.min == 60) { rtc_tmp.min = 0; rtc_tmp.hour++; }
		
		// leitura dos scancodes para movimentar mira
		dx = 0; dy = 0;
		if(scancode == 72) dy = -20;
		if(scancode == 80) dy = 20;
		if(scancode == 75) dx = -20;
		if(scancode == 77) dx = 20;
		cross1->x += dx;
		cross1->y += dy;
		
		//Verifica se mata pato
		if((scancode == 57) && ((duck1_left->x <= (cross1->x+16) && duck1_left->x+76 >= (cross1->x+16)) && (duck1_left->y <= (cross1->y+16) && duck1_left->y+56 >= (cross1->y+16)))){
			down->x = duck1_left->x;
			down->y = duck1_left->y;
			down->xspeed = duck1_left->xspeed;
			down->yspeed = duck1_left->yspeed;
			
			pato++;
			kill_duck(down, base);
			std_screen(back, base, nivel);
			i++;
		}
		else{
		
			move_cross(cross1, base);
			
			if(scancode == 57) play_song(&sshoot);
			
			if(xspeed_tmp <=0){
				move_duck(duck1_left, base);
				delay(15);
				delete_sprite(duck1_left, base);
			}
			else {
				move_duck(duck1_right, base);
				delay(15);
				delete_sprite(duck1_right, base);
			}
			
			x_tmp += xspeed_tmp;
			y_tmp += yspeed_tmp;
			
			delete_sprite(cross1, base);
		}
	}
	
	destroy_sprite(duck1_left);
	destroy_sprite(duck1_right);
	destroy_sprite(cross1);
	destroy_sprite(back);
	destroy_sprite(down);
	
	
	if(pato < 7){
		gameover = 1;
		drawStringAt("GAME OVER!", 300, 240, 1, 53, 2, base, table); }
		
	else drawStringAt("NIVEL COMPLETO!", 300, 240, 1, 53, 2, base, table);
}



